package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.mapper.DepartmentMapper;
import cn.itsource.pethome.org.query.DeparmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.basic.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DpartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        if (department.getId() == null) {
            departmentMapper.save(department);
        }else{
            this.update(department);
        }
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }

    @Override
    public PageInfo<Department> findPageByQuery(DeparmentQuery deparmentQuery) {
        //1.查出部门数据总条数
        Long total = departmentMapper.findCountByQuery(deparmentQuery);
        //2.判断是否为0条 若为0条则没有必要再查分页列表
        if(total.equals(0L)){
            return new PageInfo<Department>();
        }
        //查询分页列表
        List<Department> list = departmentMapper.findPageByQuery(deparmentQuery);
        //把总条数和分页列表，封装成PageInfo对象
        return new PageInfo<Department>(list, total);

    }


    //查询部门树
    @Override
    public List<Department> findDepartmentTree() {
        //1.查询所有的一级部门
        List<Department> departments= departmentMapper.finLevelOne();
        //2.循环所有一级部门
        for(Department department:departments){
            //3.拿到所有的二级部门
            List<Department> children = department.getChildren();
            //4.从二级部门开始迭代循环查询三级部门、、、、四级等
            this.findTree(children);
        }

        return departments;
    }


    public void  findTree(List<Department> departments){
        if(departments.size()!=0){
            for (Department department : departments) {
                //5.通过parentId查询子部门列表
                List<Department> list = departmentMapper.findByParentid(department.getId());
                if(list.size()!=0){
                    //6.把结果值查询出来后，封装到children中
                    department.setChildren(list);
                    //7.依次往下查询所有的级别(还有层级关系迭代执行)
                    this.findTree(list);
                }
            }
        }
    }
}
